package com.miracle9.game.dao;

import com.miracle9.game.entity.GoldSharkDesk;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class GoldSharkDeskDao extends SqlOperating<GoldSharkDesk, Integer> {

    public List<Integer> getDeskIdsByRoomId(int roomId) {
        return (List<Integer>) getSession().createQuery("select id from GoldSharkDesk where roomId = ?").setParameter(0, roomId).list();
    }

    @Cacheable(value = {"springCache"}, key = "'getAllGoldSharkDesk'")
    public List<GoldSharkDesk> getAllDesk() {
        return q_CreateSqlList("from GoldSharkDesk order by roomId desc,orderBy", new Object[0]);
    }

    @Cacheable(value = {"springCache"}, key = "'getGoldSharkDesk'+#id")
    public GoldSharkDesk getDesk(int id) {
        return (GoldSharkDesk) getById(Integer.valueOf(id));
    }

    @Cacheable(value = {"springCache"}, key = "'countGoldSharkDesk'+#roomId")
    public int getQuantityByRoomId(int roomId) {
        return q_CreateSqlInt("select count(*) from GoldSharkDesk where roomId=?", new Object[]{Integer.valueOf(roomId)});
    }

    @Caching(evict = {@CacheEvict(value = {"springCache"}, key = "'getGoldSharkAllDesk'"), @CacheEvict(value = {"springCache"}, key = "'countGoldSharkDesk'+#GoldSharkDesk.roomId")})
    public Integer saveReturnInteger(GoldSharkDesk GoldSharkDesk) {
        return (Integer) saveReturn(GoldSharkDesk);
    }

    @Caching(evict = {@CacheEvict(value = {"springCache"}, key = "'getGoldSharkDesk'+#GoldSharkDesk.id"), @CacheEvict(value = {"springCache"}, key = "'getAllGoldSharkDesk'")})
    public void updateDeskInfo(GoldSharkDesk GoldSharkDesk) {
        GoldSharkDesk update = getDesk(GoldSharkDesk.getId());
        update.setRoomId(GoldSharkDesk.getRoomId());
        update.setName(GoldSharkDesk.getName());
        update.setMinGold(GoldSharkDesk.getMinGold());
        update.setMinBet(GoldSharkDesk.getMinBet());
        update.setMaxBet(GoldSharkDesk.getMaxBet());
        update.setMaxZx(GoldSharkDesk.getMaxZx());
        update.setBetTime(GoldSharkDesk.getBetTime());
        update.setExchange(GoldSharkDesk.getExchange());
        update.setOnceExchangeValue(GoldSharkDesk.getOnceExchangeValue());
        update.setAnimalDiff(GoldSharkDesk.getAnimalDiff());
        update.setZxhDiff(GoldSharkDesk.getZxhDiff());
        update.setBeilvType(GoldSharkDesk.getBeilvType());
        update.setBeilvModel(GoldSharkDesk.getBeilvModel());
        update.setSiteType(GoldSharkDesk.getSiteType());
        update.setWaterType(GoldSharkDesk.getWaterType());
        update.setWaterValue(GoldSharkDesk.getWaterValue());
        update.setAutoKick(GoldSharkDesk.getAutoKick());
        update.setState(GoldSharkDesk.getState());
        update.setBetChip(GoldSharkDesk.getBetChip());
        update(update);
    }

    @Caching(evict = {@CacheEvict(value = {"springCache"}, key = "'getGoldSharkDesk'+#id"), @CacheEvict(value = {"springCache"}, key = "'getAllGoldSharkDesk'"), @CacheEvict(value = {"springCache"}, key = "'countDesk'+#roomId")})
    public void deleteById(int id, int roomId) {
        deleteById(Integer.valueOf(id));
    }

    @CacheEvict(value = {"springCache"}, key = "'getGoldSharkDesk'+#id")
    public void initializeById(int id) {
        q_CreateSql("update GoldSharkDesk set sumYaFen = 0,sumDeFen=0,sumZhxYaFen=0,sumZhxDeFen=0 where id=?", new Object[]{Integer.valueOf(id)}).executeUpdate();
    }

    @Caching(evict = {@CacheEvict(value = {"springCache"}, key = "'getAllGoldSharkDesk'")})
    public void updateOrderByById(int id, int index) {
        q_CreateSql("update GoldSharkDesk set orderBy = ? where id=?", new Object[]{Integer.valueOf(index), Integer.valueOf(id)}).executeUpdate();
    }

    public void updateStateById(int id, int state) {
        q_CreateSql("update GoldSharkDesk set state=? where id=?", new Object[]{Integer.valueOf(state), Integer.valueOf(id)}).executeUpdate();
    }

}
